<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class triggers_settingsController extends Cms_Controller_Action
{
	public function init()
	{
	  $this->view->siteTitle = 'Triggers';
	  $this->currentPage = 1;
	  $this->resultsPerPage = 8;
			
	}
	public function indexAction()
	{
	  	
			
	}
	public function changetriggerSettingsAction()
	{
		$memid = $_SESSION["sess_userid"];
		//echo '<pre>';print_r($_POST);exit;
	  		$type = $_POST['type'];
			$action = $_POST['action'];
			if($type == 1)
			{
				$setting_type="Twitter";
			}
			else if($type == 2)
			{
				$setting_type="LinkedIn";
			}
			else
			{
				$setting_type="Facebook";
			}
				$result= Doctrine::getTable('TriggerSettings')->getSettings($memid,$setting_type);
				if(count($result)!=0)
				{
					$result= Doctrine::getTable('TriggerSettings')->updateSettings($result['id'],$action);
				}
				else
				{
					$result= Doctrine::getTable('TriggerSettings')->addSettings($memid,$setting_type,$action);
				}
				$jsonData['output'] = $result;
				$this->_helper->json->sendJson($jsonData);	
			
	}
	public function triggersSettingsAction()
	{
			$memid = $_SESSION["sess_userid"];
			$type1="Twitter";
			$type2="LinkedIn";
			$type3="Facebook";
			$result1= Doctrine::getTable('TriggerSettings')->getSettings($memid,$type1);
			$result2= Doctrine::getTable('TriggerSettings')->getSettings($memid,$type2);
			$result3= Doctrine::getTable('TriggerSettings')->getSettings($memid,$type3);
			//echo '<pre>';print_r($result1);exit;
			$this->view->twitter=$result1;
			$this->view->linkedIn=$result2;
			$this->view->facebook=$result3;
			
			
	}
	public function triggersNoticesAction()
	{
	  	
			$memid = $_SESSION["sess_userid"];
			$is_user= Doctrine::getTable('Notifications')->getUserId($memid);
			//echo '<pre>';print_r($is_user);exit;
			if($is_user['numCount']!=0)
			{
			$result= Doctrine::getTable('Notifications')->getNotifications($memid);
			}
			else
			$result= Doctrine::getTable('Notifications')->getNotificationss($memid);
			//echo '<pre>';print_r($result);exit;
			$this->view->Notifications=$result;
			
	}
	public function saveNotificationsAction()
	{
			//echo '<pre>';print_r($_POST);exit;
			$memid = $_SESSION["sess_userid"];
			$result= Doctrine::getTable('Notifications')->getNotifications($memid);
			//echo '<pre>';print_r($result['user_id']);exit;
			if($result['user_id']== $_SESSION["sess_userid"])
			{
				//echo '<pre>';print_r($_POST);exit;
				$result= Doctrine::getTable('Notifications')->updateNotifications($memid,$_POST);
				
			}
			else
			{
				$result= Doctrine::getTable('Notifications')->addNotifications($memid,$_POST);
				
				
			}
			$this->_redirect("triggers/settings/triggers-notices");
					
	}
	//////// Group actionss ###############################################
	
	public function groupsAction()
	{
	  	
			$memid = $_SESSION["sess_userid"];
			$group_type="Triggers";
			$result = Doctrine::getTable('Groups')->getGroupDetails($memid,$group_type,$currentPage,$resultPerPage);
			//echo '<pre>';print_r($result);exit;
			$count=count($result['result']);
			$this->view->total=$count;
			$this->view->result=$result;
			
			
	}
	public function groupaddpopupAction()
	{
	  	
			$memid = $_SESSION["sess_userid"];
			
			
	}
	public function addgroupAction()
	{
	    //echo '<pre>';print_r($_POST);print_r($_FILES);exit;
			$name = $_POST['Name'];
			$desc = $_POST['message'];
			$size = $_FILES['size'];
			$group_type="Triggers";
			$imagesDir = '../'.$this->CONFIG->path->groupLogos;
			if($_FILES['file']['name']!="")
			{
				
				if($size>300000)
				{
				echo "error file size > 3 MB";
				unlink($_FILES['file']['tmp_name']);
				exit;
				}
				$fileName = str_replace(' ', '_', $_FILES['file']['name']);
				$fsname=date('Ymdhis').'_-_'.$fileName;
				//echo $fsname;exit;
				move_uploaded_file($_FILES['file']['tmp_name'],$imagesDir.$fsname);				
			}
		//echo $name,$desc;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('Groups')->getGroupName($memid,$name,$group_type);
		//echo $result[numCount]);exit;
		if($result[numCount]==0)
		{
		$r2 = Doctrine::getTable('Groups')->addGroup($memid,$name,$desc,$fsname,$group_type);
		//echo $r2;exit;
		}
		$this->_redirect("/triggers/settings/groups");
	}
	public function editgroupAction()
	{
		$this->_helper->layout->disableLayout();
		$group_id=$this->_getParam("val");
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('Groups')->getGroupDetailsByid($memid,$group_id);
		//echo '<pre>';print_r($result);exit;
			$this->view->result=$result;
			$this->view->group_id=$group_id;
	}	
	public function updategroupAction()
	{
	    //echo '<pre>';print_r($_POST);print_r($_FILES);exit;
			$memid = $_SESSION["sess_userid"];
			$group_id=$this->_getParam('id');
			$name = $_POST['groupname'];
			$desc = $_POST['messageText'];
			$size = $_FILES['size'];
			$imagesDir = '../'.$this->CONFIG->path->groupLogos;
			if($_FILES['file']['name']!="")
			{
				
				if($size>300000)
				{
				echo "error file size > 3 MB";
				unlink($_FILES['file']['tmp_name']);
				exit;
				}
				$fileName = str_replace(' ', '_', $_FILES['file']['name']);
				$fsname=date('Ymdhis').'_-_'.$fileName;
				//echo $fsname;exit;
				move_uploaded_file($_FILES['file']['tmp_name'],$imagesDir.$fsname);	
			$r2 = Doctrine::getTable('Groups')->updateGroup($memid,$name,$desc,$fsname,$group_id);
			//echo $r2;exit;
							
			}
			else
			{
				$r2 = Doctrine::getTable('Groups')->updateGroups($memid,$name,$desc,$group_id);
			}
			$this->_redirect("/triggers/settings/groups");
		//echo $name,$desc;exit;
		
		
			
	}
	public function groupviewAction()
	{
		$group_id=base64_decode($this->_getParam('id'));
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$result = Doctrine::getTable('GroupDetails')->getGroupDetailsByid($group_id);
		//echo '<pre>';print_r($result);exit;
		$count=count($result);
		$this->view->count=$count;
		$this->view->group_id=$group_id;
		$this->view->result=$result;
			
	}	
	public function editgroupdetailsAction()
	{
		$this->_helper->layout->disableLayout();
		$id=$this->_getParam("val");
		$group_id=$this->_getParam("group_id");
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('GroupDetails')->getDetailsByid($id);
		//echo '<pre>';print_r($result);exit;
		$this->view->id = $id;
		$this->view->group_id = $group_id;
		$this->view->result=$result;
	}	
	public function updategroupdetailsAction()
	{
		$id=$this->_getParam("id");
		//echo $id;exit;
		$group_id=$this->_getParam('current_page');
		//echo $group_id;exit;
		$name=$_POST['name'];
		$email=$_POST['email'];
		$memid = $_SESSION["sess_userid"];
		$result = Doctrine::getTable('GroupDetails')->updateGroupDetails($id,$name,$email);
		//echo '<pre>';print_r($result);exit;
		$this->_redirect("triggers/settings/groupview/".$group_id);
	}
	

}	
